package zuoye;

import util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;

public class Transferjpanel extends JPanel implements ActionListener {
    private Icon sureimage = Util.getIcon("/images/buttonSure.jpg");
    private Icon offimage = Util.getIcon("/images/buttonOff.jpg");
    private JButton SurejButton;
    private JButton OffjButton;
    private Mainjframe frame;
    private JTextField textField1;
    private JTextField textField2;
    private JLabel jLabel;
    private JLabel jLabel1;
    private boolean result = false;
    private int dindex = 0;

    public Transferjpanel(Mainjframe frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        jLabel = new JLabel("请输入收款人姓名：");
        jLabel.setForeground(Color.BLACK);
        jLabel.setFont(new Font("宋体", Font.BOLD, 22));
        jLabel.setBounds(340, 220, 200, 40);
        this.add(jLabel);

        textField1 = new JTextField();
        textField1.setBounds(550, 220, 350, 40);
        this.add(textField1);

        jLabel1 = new JLabel("请输入转账金额：");
        jLabel1.setForeground(Color.BLACK);
        jLabel1.setFont(new Font("宋体", Font.BOLD, 22));
        jLabel1.setBounds(340, 280, 200, 40);
        this.add(jLabel1);

        textField2 = new JTextField();
        textField2.setBounds(550, 280, 350, 40);
        this.add(textField2);

        SurejButton = new JButton(sureimage);
        SurejButton.setBounds(360, 500, 140, 50);
        this.add(SurejButton);
        SurejButton.addActionListener(this);
        OffjButton = new JButton(offimage);
        OffjButton.setBounds(700, 500, 140, 50);
        this.add(OffjButton);
        OffjButton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        GetConnectionUtil get = new GetConnectionUtil();
        Connection con = get.getConnection();
        try {
            con.setAutoCommit(false);
            if (e.getSource() == SurejButton) {
                GetConnectionUtil g = new GetConnectionUtil();
                String other = textField1.getText();
                User otheruser = g.getByUsername(other);
                User loginuser = g.getById(frame.getLoginUser().getId());
                double blance = Double.valueOf(textField2.getText());
                double userblance = loginuser.getMoney();
                if (other.equals(loginuser.getUsername())) {
                    JOptionPane.showMessageDialog(this, "不能转账给自己");
                    return;
                }
                if (otheruser == null) {
                    JOptionPane.showMessageDialog(this, "查无此人");
                    return;
                }
                if (blance > userblance) {
                    JOptionPane.showMessageDialog(this, "余额不足");
                    return;
                }
                JOptionPane.showMessageDialog(this, "您本次转账" + blance + "元");
                loginuser.setMoney(userblance - blance);
                g.update(loginuser);
                otheruser.setMoney(otheruser.getMoney() + blance);
                g.update(otheruser);
                textField1.setText("");
                textField2.setText("");
                con.commit();
                frame.switchPanel("menu");
            } else if (e.getSource() == OffjButton) {
                frame.switchPanel("menu");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
        }
    }
}
